var callBack = null,
	pickFileList = [],
	maxSelect = 1,
	fileType = [
		'image/jpeg',
		'image/png',
		'image/bmp',
		'image/gif',
		'application/vnd.ms-excel',
		'application/msword',
		'application/x-zip-compressed',
		'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
		'application/vnd.ms-excel',
		'application/pdf',
		'application/vnd.ms-powerpoint',
		'application/vnd.openxmlformats-officedocument.presentationml.presentation',
		'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		'text/plain'
	];

handlePickFile = (mlist, pickMax, pickPid, pickInputId, fileTableId, fileTableName, pickIndex, e, callback) => {
	pickFileList = mlist;
	maxSelect = pickMax;
	callBack = callback;

	let pickId = e.currentTarget.id;
	let files = e.currentTarget.files; //获取的图片文件

	if (files && files.length > 0) {
		for (var i = 0; i < files.length; i++) {
			let fileItem = files[i];
			let fileName = fileItem.name;
			let type = fileItem.type;
			let size = fileItem.size;
			let isImage = type.indexOf('image');
			if (fileType.includes(type)) {
				if (size > 50 * 1024 * 1024) {
					$(`#${pickInputId}`).val(''); //加上这行代码可以重复选择相同的文件或者照片
					showTipsDialog("您选择的文件超过50M了，请重新选择!");
					return
				}
				uploadPickFile(fileItem, pickPid, pickInputId, isImage);
			} else {
				$(`#${pickInputId}`).val(''); //加上这行代码可以重复选择相同的文件或者照片
				showTipsDialog('暂不支持此类型文件上传，请重新选择！');
			}
		}
	} else {
		showTipsDialog('文件异常，请重新选择！');
	}
}

uploadPickFile = (fileItem, pickPid, pickInputId, isImage) => {
	let tempFilePath = 'supervise/file/' + fileItem.name + '/' + new Date().getTime();
	let fileObj = {
		path: tempFilePath,
		name: fileItem.name,
		size: fileItem.size,
		type: fileItem.type
	};
	if (isImage == -1) { //不是img
		let textHtml = `<div class="file_add" onclick="onDeletePickFile('${toObjectStr(fileObj)}',this,'${pickPid}')">
							<div class="file_delete"></div>
							<span class="file_text">${fileItem.name}</span>
						</div>`;
		$(`#${pickPid}`).before(textHtml);
		$(`#${pickInputId}`).val('');
	} else {
		let reader = new FileReader();
		reader.onload = function(evt) {
			let resData = evt.target.result;
			let startIndex = resData.indexOf(',');
			let base64File = resData.substring(startIndex + 1, resData.length);
			let imgHtml = `<div class="file_upload_box" onclick="onDeletePickFile('${toObjectStr(fileObj)}',this,'${pickPid}')">
								<img src="../../img/ic_clear.png" class="delete" />
								<img src="${resData}" class="file_img"/>
							</div>`;
			$(`#${pickPid}`).before(imgHtml);
			$(`#${pickInputId}`).val('');
		}
		reader.readAsDataURL(fileItem);
	}
	pickFileList.push(fileObj);
	callBack(pickFileList);
	handleAddFileBtn(pickPid);
}

onDeletePickFile = (e, ele, pickInputId) => {
	let item = toObject(e);
	showActionDialog('删除提示', '您确定要删除该文件吗？', res => {
		$(ele).remove();
		let index = pickFileList.findIndex(obj => obj.path === item.path)
		if (index != -1) {
			showToast('删除成功！');
			pickFileList.splice(index, 1);
			callBack(pickFileList);
			handleAddFileBtn(pickInputId);
		}
	});
}

handleAddFileBtn = (pickInputId) => {
	if (pickFileList.length == maxSelect) {
		$(`#${pickInputId}`).hide();
	} else {
		$(`#${pickInputId}`).show();
	}
}
